package com.topscomm.file.util;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyStore;
import java.util.Calendar;
import java.util.UUID;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;

public class Utils {
	public static boolean isEmpty(String str) {
		if (null == str || str.length() == 0) {
			return true;
		}
		return false;
	}

	public static boolean isNotEmpty(String str) {
		if (null != str && str.trim().length() > 0) {
			return true;
		}
		return false;
	}

	public static final String CERT_TYPE_P12 = "PKCS12";
	public static final String CERT_TYPE_JKS = "JKS";

	public static KeyManager getKeyManager(String path, String password) throws Exception {
		if (isEmpty(path)) {
			return null;
		}
		String type = CERT_TYPE_P12;
		if (path.toUpperCase().endsWith(CERT_TYPE_JKS)) {
			type = CERT_TYPE_JKS;
		}
		KeyStore keyStore = KeyStore.getInstance(type);
		keyStore.load(loadFile(path), password.toCharArray());
		KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
		kmf.init(keyStore, password.toCharArray());
		KeyManager[] km = kmf.getKeyManagers();
		return km[0];
	}

	public static TrustManager getTrustManager(String path, String password) throws Exception {
		KeyStore keyStore = KeyStore.getInstance("JKS");
		keyStore.load(loadFile(path), password.toCharArray());
		TrustManagerFactory tf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
		tf.init(keyStore);
		TrustManager[] tm = tf.getTrustManagers();
		return tm[0];
	}

	public static final String classpathFlag = "classpath:";

	public static InputStream loadFile(String filePath) throws Exception {
		if (filePath.startsWith(classpathFlag)) {
			return Utils.class.getClassLoader().getResourceAsStream(filePath.replace(classpathFlag, ""));
		} else {
			return new FileInputStream(filePath);
		}
	}

	/**
	 * 从文件名获取扩展名
	 *
	 * @param fileName
	 * @return
	 */
	public static String getExtension(String fileName) {
		if (Utils.isEmpty(fileName)) {
			return null;
		}
		return fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
	}

	/**
	 * 按年月组织文件 构造文件路径
	 */
	public static String buildFilePath(String fileId) {
		return getYearMonth() + "/" + fileId;
	}

	/**
	 * 产生新文件名
	 */
	public static String newFileName(String fileName) {
		return UUID.randomUUID() + "_" + getExtension(fileName);
	}

	private static String getYearMonth() {
		Calendar instance = Calendar.getInstance();
		int year = instance.get(Calendar.YEAR);
		int month = instance.get(Calendar.MONTH) + 1;
		int day = instance.get(Calendar.DAY_OF_MONTH);
		return year + "/" + month + "/" + day;
	}

	/**
	 * 获取文件名
	 *
	 * @param file
	 * @return
	 */
	public static String getFileName(String file) {
		int index = file.lastIndexOf("/") + 1;
		if (index > -1) {
			return file.substring(index);
		} else {
			return new File(file).getName();
		}
	}

	public static int copyStream(InputStream inputStream, OutputStream outputStream, int buffSize) {

		int sumLegth = 0;
		try {
			byte[] buffer = new byte[buffSize];
			int len = 0;

			while ((len = inputStream.read(buffer)) > 0) {
				outputStream.write(buffer, 0, len);
				sumLegth += len;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			Utils.close(inputStream);
			// Utils.close(outputStream);
		}
		return sumLegth;

	}

	public static void close(Closeable closeable) {
		try {
			closeable.close();
		} catch (IOException e) {
		}
	}

}
